﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.Framework.Common;
using Super.Framework.Common.Files;
using Super.Framework.Common.Secure;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using Developer.Service;
using Developer.Entity;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class UserController : BaseController
    {
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            IEnumerable<Users> users = UserService.GetAll().Where(a => a.IsAbort == false); ;
            
            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.Account.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePassword()
        {
            var userId = Query.Value("Id").ToInt();
            var user = new Users();
            if (userId > 0)
            {
                user = UserService.GetAll().FirstOrDefault(a => a.Id == userId && a.IsAbort == false);
            }
            return View(user);
        }
        [HttpPost]
        public ActionResult ChangePassword(FormCollection fc)
        {
            var newPassword = Query.Value("newPassword");
            var userId = Query.Value("Id").ToInt();
            if (userId > 0)
            {
                var user = UserService.GetAll().FirstOrDefault(a => a.Id == userId && a.IsAbort == false);
                if (user != null)
                {
                    user.Password = newPassword.Md5String();
                    UserService.Change(user);
                    ShowMsg("操作成功", MessageItemType.Right);
                    return Redirect("/admin/user/changepassword/?Id=" + userId);
                }
            }
            ShowMsg("操作出错", MessageItemType.Error);
            return Redirect("/admin/user/changepassword");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                UserService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/user/index");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/user/index");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public bool BatDelete()
        {
            var typeIds = Query.Value("ids[]").Split(',');
            if (typeIds.Length < 1) return false;
            foreach (var t in typeIds)
            {
                UserService.Remove(t.ToInt());
            }
            return true;
        }
        /// <summary>
        /// 会员详情
        /// </summary>
        /// <returns></returns>
        public ActionResult Detail()
        {
            var userId = Query.Value("Id").ToInt();
            var user = new Users();
            if (userId > 0)
            {
                user = UserService.GetAll().FirstOrDefault(a => a.IsAbort == false && a.Id == userId);
            }
            return View(user);
        }

        /// <summary>
        /// 微信详情
        /// </summary>
        /// <returns></returns>
        public ActionResult WechatDetail()
        {
            var wid = Query.Value("Id").ToInt();
            var wechat = new WeChats();
            if (wid > 0)
            {
                wechat = WeChatService.GetAll().FirstOrDefault(a=>a.Id == wid);
            }
            return View(wechat);
        }
    }
}